﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using Shl01.Models;
using Shl01.Dal;

namespace Shl01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.panel1.AutoScroll = true;
            this.comboBox3.Text = "SQL Server 身份验证";
        }

        public void Fang()
        {
            Conn conn = new Conn();
            conn.Fuwu = this.textBox1.Text;
            conn.Type = this.comboBox3.Text == "SQL Server 身份验证" ? 0 : 1;
            conn.Id = this.textBox2.Text;
            conn.Pwd = this.textBox3.Text;
            try
            {
                Fang f = new Fang();
                this.comboBox1.DisplayMember = "database_name";
                this.comboBox1.DataSource = f.GetAllDatabase(conn);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Ma();
        }
        public void Ma()
        {
            Conn conn = new Conn();
            conn.Fuwu = this.textBox1.Text;
            conn.Type = this.comboBox3.Text == "SQL Server 身份验证" ? 0 : 1;
            conn.Id = this.textBox2.Text;
            conn.Pwd = this.textBox3.Text;
            try
            {
                this.panel1.Controls.Clear();
                Fang f = new Fang();
                DataTable dt = f.GetAllTables(conn, this.comboBox1.Text);
                int X = 0;
                int Y = 1;
                foreach (DataRow dr in dt.Rows)
                {
                    CheckBox ckb = new CheckBox();
                    ckb.Text = dr["table_Name"].ToString();
                    ckb.AutoSize = true;
                    ckb.Location = new Point(5 + X * 150, Y * 30);
                    this.panel1.Controls.Add(ckb);
                    if (X == 2)
                    {
                        X = 0;
                        Y++;
                    }
                    else
                    {
                        X++;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.checkBox1.Checked = false;
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBox3.Text != "SQL Server 身份验证")
            {
                this.textBox2.Enabled = false;
                this.textBox3.Enabled = false;
            }
            else
            {
                this.textBox2.Enabled = true;
                this.textBox3.Enabled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Fang();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Conn conn = new Conn();
            conn.Fuwu = this.textBox1.Text;
            conn.Type = this.comboBox3.Text == "SQL Server 身份验证" ? 0 : 1;
            conn.Id = this.textBox2.Text;
            conn.Pwd = this.textBox3.Text;
            SqlForm sql = new SqlForm();
            sql.DataBase = this.comboBox1.Text;
            sql.Conn = conn;
            sql.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (CheckBox cb in this.panel1.Controls)
            {
                if (cb.Checked)
                {
                    list.Add(cb.Text);
                }
            }
            Conn conn = new Conn();
            conn.Fuwu = this.textBox1.Text;
            conn.Type = this.comboBox3.Text == "SQL Server 身份验证" ? 0 : 1;
            conn.Id = this.textBox2.Text;
            conn.Pwd = this.textBox3.Text;
            try
            {
                string path = this.txt_path.Text + "\\" + this.comboBox1.Text + "\\Models";
                Directory.CreateDirectory(path);
                Class1 c = new Class1();
                c.Ma(conn, this.comboBox1.Text, path, list);
                MessageBox.Show("完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (CheckBox cb in this.panel1.Controls)
            {
                if (cb.Checked)
                {
                    list.Add(cb.Text);
                }
            }
            Conn conn = new Conn();
            conn.Fuwu = this.textBox1.Text;
            conn.Type = this.comboBox3.Text == "SQL Server 身份验证" ? 0 : 1;
            conn.Id = this.textBox2.Text;
            conn.Pwd = this.textBox3.Text;
            try
            {
                string path = this.txt_path.Text + "\\" + this.comboBox1.Text + "\\DAL";
                Directory.CreateDirectory(path);
                ServiceDal s = new ServiceDal();
                s.Ma(conn, this.comboBox1.Text, path, list,this.checkBox2.Checked);
                MessageBox.Show("完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = "SQLHelper.cs";
            if (save.ShowDialog() == DialogResult.OK)
            {
                using (FileStream fs = new FileStream(save.FileName, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(SQLHelper.GetStr(Application.StartupPath + "\\SQLHelper.txt"));
                        MessageBox.Show("完成");
                    }
                }

            }

        }

        private void button6_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = "Comm.cs";
            if (save.ShowDialog() == DialogResult.OK)
            {
                using (FileStream fs = new FileStream(save.FileName, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(SQLHelper.GetStr(Application.StartupPath + "\\Comm.txt"));
                        MessageBox.Show("完成");
                    }
                }

            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (CheckBox cb in this.panel1.Controls)
            {
                if (cb.Checked)
                {
                    list.Add(cb.Text);
                }
            }
            Conn conn = new Conn();
            conn.Fuwu = this.textBox1.Text;
            conn.Type = this.comboBox3.Text == "SQL Server 身份验证" ? 0 : 1;
            conn.Id = this.textBox2.Text;
            conn.Pwd = this.textBox3.Text;
            try
            {
                string path = this.txt_path.Text + "\\" + this.comboBox1.Text + "\\BLL";
                Directory.CreateDirectory(path);
                BllDa s = new BllDa();
                s.Ma(conn, this.comboBox1.Text, path, list);
                MessageBox.Show("完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                foreach (CheckBox cb in this.panel1.Controls)
                {
                    cb.Checked = true;
                }
            }
            else
            {
                foreach (CheckBox cb in this.panel1.Controls)
                {
                    cb.Checked = false;
                }
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBox2.Focus();
            }

            if (e.KeyCode == Keys.Escape)
            {
                Application.Exit();
            }
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBox3.Focus();
            }

            if (e.KeyCode == Keys.Escape)
            {
                Application.Exit();
            }
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                Fang();
            }

            if (e.KeyCode == Keys.Escape)
            {
                Application.Exit();
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = "IBaseService.cs";
            if (save.ShowDialog() == DialogResult.OK)
            {
                using (FileStream fs = new FileStream(save.FileName, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(SQLHelper.GetStr(Application.StartupPath + "\\IBaseDAL.txt"));
                        MessageBox.Show("完成");
                    }
                }

            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = "IBaseManager.cs";
            if (save.ShowDialog() == DialogResult.OK)
            {
                using (FileStream fs = new FileStream(save.FileName, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(SQLHelper.GetStr(Application.StartupPath + "\\IBaseManager.txt"));
                        MessageBox.Show("完成");
                    }
                }
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = "DbContextFactory.cs";
            if (save.ShowDialog() == DialogResult.OK)
            {
                using (FileStream fs = new FileStream(save.FileName, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(SQLHelper.GetStr(Application.StartupPath + "\\DbContextFactory.txt"));
                        MessageBox.Show("完成");
                    }
                }
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = "BaseService.cs";
            if (save.ShowDialog() == DialogResult.OK)
            {
                using (FileStream fs = new FileStream(save.FileName, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(SQLHelper.GetStr(Application.StartupPath + "\\BaseService.txt"));
                        MessageBox.Show("完成");
                    }
                }
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = "BaseManager.cs";
            if (save.ShowDialog() == DialogResult.OK)
            {
                using (FileStream fs = new FileStream(save.FileName, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(SQLHelper.GetStr(Application.StartupPath + "\\BaseManager.txt"));
                        MessageBox.Show("完成");
                    }
                }
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (CheckBox cb in this.panel1.Controls)
            {
                if (cb.Checked)
                {
                    list.Add(cb.Text);
                }
            }
            Conn conn = new Conn();
            conn.Fuwu = this.textBox1.Text;
            conn.Type = this.comboBox3.Text == "SQL Server 身份验证" ? 0 : 1;
            conn.Id = this.textBox2.Text;
            conn.Pwd = this.textBox3.Text;
            try
            {
                string path = this.txt_path.Text + "\\" + this.comboBox1.Text + "\\IBLL";
                Directory.CreateDirectory(path);
                IBLL s = new IBLL();
                s.Ma(conn, this.comboBox1.Text, path, list);
                MessageBox.Show("完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (CheckBox cb in this.panel1.Controls)
            {
                if (cb.Checked)
                {
                    list.Add(cb.Text);
                }
            }
            Conn conn = new Conn();
            conn.Fuwu = this.textBox1.Text;
            conn.Type = this.comboBox3.Text == "SQL Server 身份验证" ? 0 : 1;
            conn.Id = this.textBox2.Text;
            conn.Pwd = this.textBox3.Text;
            try
            {
                string path = this.txt_path.Text + "\\" + this.comboBox1.Text + "\\IDAL";
                Directory.CreateDirectory(path);
                IDAL s = new IDAL();
                s.Ma(conn, this.comboBox1.Text, path, list);
                MessageBox.Show("完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (CheckBox cb in this.panel1.Controls)
            {
                if (cb.Checked)
                {
                    list.Add(cb.Text);
                }
            }
            Conn conn = new Conn();
            conn.Fuwu = this.textBox1.Text;
            conn.Type = this.comboBox3.Text == "SQL Server 身份验证" ? 0 : 1;
            conn.Id = this.textBox2.Text;
            conn.Pwd = this.textBox3.Text;
            try
            {
                string path = this.txt_path.Text + "\\" + this.comboBox1.Text + "\\DAL";
                Directory.CreateDirectory(path);
                DAL s = new DAL();
                s.Ma(conn, this.comboBox1.Text, path, list);
                MessageBox.Show("完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (CheckBox cb in this.panel1.Controls)
            {
                if (cb.Checked)
                {
                    list.Add(cb.Text);
                }
            }
            Conn conn = new Conn();
            conn.Fuwu = this.textBox1.Text;
            conn.Type = this.comboBox3.Text == "SQL Server 身份验证" ? 0 : 1;
            conn.Id = this.textBox2.Text;
            conn.Pwd = this.textBox3.Text;
            try
            {
                string path = this.txt_path.Text + "\\" + this.comboBox1.Text + "\\BLL";
                Directory.CreateDirectory(path);
                BLL s = new BLL();
                s.Ma(conn, this.comboBox1.Text, path, list);
                MessageBox.Show("完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (CheckBox cb in this.panel1.Controls)
            {
                if (cb.Checked)
                {
                    list.Add(cb.Text);
                }
            }
            Conn conn = new Conn();
            conn.Fuwu = this.textBox1.Text;
            conn.Type = this.comboBox3.Text == "SQL Server 身份验证" ? 0 : 1;
            conn.Id = this.textBox2.Text;
            conn.Pwd = this.textBox3.Text;
            try
            {
                string path = this.txt_path.Text + "\\" + this.comboBox1.Text + "\\BLL";
                Directory.CreateDirectory(path);
                WarnBLL s = new WarnBLL();
                s.Ma(conn, this.comboBox1.Text, path, list);
                MessageBox.Show("完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (CheckBox cb in this.panel1.Controls)
            {
                if (cb.Checked)
                {
                    list.Add(cb.Text);
                }
            }
            Conn conn = new Conn();
            conn.Fuwu = this.textBox1.Text;
            conn.Type = this.comboBox3.Text == "SQL Server 身份验证" ? 0 : 1;
            conn.Id = this.textBox2.Text;
            conn.Pwd = this.textBox3.Text;
            try
            {
                string path = this.txt_path.Text + "\\" + this.comboBox1.Text + "\\IBLL";
                Directory.CreateDirectory(path);
                IBLLWarn s = new IBLLWarn();
                s.Ma(conn, this.comboBox1.Text, path, list);
                MessageBox.Show("完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_open_Click(object sender, EventArgs e)
        {

            FolderBrowserDialog folder = new FolderBrowserDialog();
            DialogResult result = folder.ShowDialog();
            if (result != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }
            this.txt_path.Text = folder.SelectedPath;
        }

        private void button19_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = "ErrorVm.cs";
            if (save.ShowDialog() == DialogResult.OK)
            {
                using (FileStream fs = new FileStream(save.FileName, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(SQLHelper.GetStr(Application.StartupPath + "\\ErrorVm.cs"));
                        MessageBox.Show("完成");
                    }
                }

            }
        }
    }
}
